// pages/myself/myself.js
let network = null;
let utils = null;
let api = null;
let storage = null;
var Zan = require('../../dist/index');
//var storage = require('../../utils/storage.js');
const app = getApp();

Page(Object.assign({}, Zan.TopTips, {

  /**
   * 页面的初始数据
   */
  data: {
    balance: 0,
    integral: 0,
    couponCount: 0,
    userAndTenantInfo: null,
    avatarUrl: null,
    nickName: null,
    mobile: null,
    saleInfo: null,
    isSign: '未签到',
    formId: '',
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseMobile: wx.canIUse('button.open-type.getPhoneNumber')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
    storage = new app.Storage();

    if (app.globalData.userAndTenantInfo) {
      this.setData({
        avatarUrl: app.globalData.userAndTenantInfo.avatarUrl,
        nickName: app.globalData.userAndTenantInfo.nickName,
      })
    } else {
      app.userAndTenantInfoCallback = res => {
        this.setData({
          avatarUrl: res.avatarUrl,
          nickName: res.nickName,
        })
      }
    }

    wx.getUserInfo({
      success: res => {
        this.setData({
          avatarUrl: res.userInfo.avatarUrl,
          nickName: res.userInfo.nickName,
        })
      }
    })

    network._post_json(this, api.getSaleInfo, {}, saleInfoSuccess);
    network._post_json(this, api.isSign, {}, isSignSuccess);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    /**
    * 获取个人及商家信息
    */
    if (app.globalData.userAndTenantInfo.mobile) {
      this.setData({
        mobile: app.globalData.userAndTenantInfo.mobile.substr(0, 3) + "****" + app.globalData.userAndTenantInfo.mobile.substr(7)
      })
    }

    if (app.globalData.userAndTenantInfo) {
      this.setData({
        userAndTenantInfo: app.globalData.userAndTenantInfo
      })
    } else {
      app.userAndTenantInfoCallback = res => {
        this.setData({
          userAndTenantInfo: res
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    app.globalData.timeStamp = app.globalData.timeStamp + 500;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  // submit: function (e) {
  //   network._post_json(this, api.saveFormId, { formId: e.detail.formId }, fromIdSuccess);
  // },

  /**
    * 如果用户已授权，并且用户昵称不为空，则直接弹框
    */
  getUserInfo: function (e) {
    if (utils.canTap(e)) {
      var page = e.currentTarget.dataset.page;
      app.globalData.userInfo = e.detail.userInfo
      if (app.globalData.userInfo) {
        wx.navigateTo({
          url: page,
        })

        // 如果用户昵称为空，并且授权成功，则修改用户资料
        if (this.data.nickName == e.detail.userInfo.nickName) {
          console.log('++++++++++++++')
        } else {
          console.log('==============')
          network._post_json(this, api.modifyUserInfo, { nickName: e.detail.userInfo.nickName, avatarUrl: e.detail.userInfo.avatarUrl, gender: e.detail.userInfo.gender }, success);
        }
      }
    }
  },

  formSubmit: function (e) {
    this.setData({
      formId: e.detail.formId
    })
  },

  getPhoneNumber: function (e) {
    if (utils.canTap(e)) {
      if (e.detail.encryptedData) {
        network._post_json(this, api.wxphone, { encryptedData: e.detail.encryptedData, iv: e.detail.iv, sessionKey: app.globalData.sessionKey }, bindSuccess);
      }
    }
  },

  goQrcode: function (e) {
    if (utils.canTap(e)) {
      wx.navigateTo({
        url: '/pages/myself/commission/Qrcode'
      })
    }
  },

  goOrderShop: function (e) {
    if (utils.canTap(e)) {
      wx.navigateTo({
        url: '/pages/myself/store/OrderShop'
      })
    }
  },

  goShopInfo: function (e) {
    if (utils.canTap(e)) {
      wx.navigateTo({
        url: '/pages/myself/store/StoreInfo'
      })
    }
  },

  goCustomerService: function (e) {
    if (utils.canTap(e)) {
      wx.navigateTo({
        url: '/pages/myself/commission/CustomerService'
      })
    }
  },

  goAbout: function (e) {
    if (utils.canTap(e)) {
      wx.navigateTo({
        url: '/pages/myself/about/About'
      })
    }
  },

  gointegral: function (e) {
    if (utils.canTap(e)) {
      wx.navigateTo({
        url: '/pages/myself/integral/Integral'
      })
    }
  },
  goMySpellGroup: function (e) {
    if (utils.canTap(e)) {
      wx.navigateTo({
        url: '/pages/myself/myspellgroup/MySpellGroup?index=0',
      })
    }
  },
  jinrijiandao: function () {
    network._post_json(this, api.signSendIntegral, {}, signSuccess);
  }

}))

var success = function (that, res) {
  that.setData({
    avatarUrl: app.globalData.userInfo.avatarUrl,
    nickName: app.globalData.userInfo.nickName
  });
}

var bindSuccess = function (that, res) {
  if (that.data.formId && app.globalData.userAndTenantInfo.mobile != res.data.data.mobile) {
    network._post_json(that, api.sendCoupn, { formId: that.data.formId, type: 2 }, sendCoupnSuccess);
  }

  app.globalData.userAndTenantInfo.mobile = res.data.data.mobile;
  that.setData({
    mobile: res.data.data.mobile.substr(0, 3) + "****" + res.data.data.mobile.substr(7)
  })


}

// 注水数据
var saleInfoSuccess = function (that, res) {
  if (res.data.data) {
    that.setData({
      saleInfo: res.data.data,
    })
  }
}

// 签到成功
var signSuccess = function (that, res) {
  if (res.data.data) {
    storage.setSign();
    that.setData({
      isSign: '已签到'
    })
  }
}

var isSignSuccess = function (that, res) {
  if (res.data.data) {
    that.setData({
      isSign: res.data.data
    })
  }
}

var fromIdSuccess = function (that, res) { }

var sendCoupnSuccess = function (that, res) { }
